package com.vinaprime.primeschedule.presentation;

import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.vinaprime.primeschedule.businesslayer.VPContact_BL;
import com.vinaprime.primeschedule.entities.VPContact;

public class VPContactListCheckActivity extends Activity {

	public final static String CONTACT_LIST_CHECK = "com.vinaprime.primeschedule.presentation.MESSAGE";
	public final static String CONTACT_LIST_START = "com.vinaprime.primeschedule.presentation.MESSAGE";
	
	MyCheckListAdapter dataAdapter = null;
	
	List<VPContact> listContactView = null;
	List<VPContact> listContactCheck;
    ListView listView;
    static final int LIST_VIEW_ID = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_vpcontact_list_check);
		ActionBar actionBar = getActionBar();
		Drawable d=getResources().getDrawable(R.raw.background_top);  
		actionBar.setBackgroundDrawable(d);
		actionBar.setIcon(null);
		actionBar.setDisplayUseLogoEnabled(false);
		actionBar.setHomeButtonEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		//-----------------------------------//
		
		showListItem();

		
	}

	@SuppressWarnings("unchecked")
	public void showListItem()
	{	 
		
			Intent myIntentFather = getIntent();
			listContactView = (List<VPContact>)myIntentFather.getSerializableExtra(CONTACT_LIST_START);
			
			  dataAdapter = new MyCheckListAdapter(this,
			    R.layout.activity_vpcontact_sub_check, (ArrayList<VPContact>)listContactView);
			  ListView listView = (ListView) findViewById(R.id.listView_Contact_Check);
			  
			  listView.setAdapter(dataAdapter);

	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.action_bar_save_cancel, menu);
		return true;
	}
	
	@Override
	  public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case R.id.action_save:
	    	//------------
	    	listContactCheck = null;
	    	ArrayList<VPContact> contactSend = new  ArrayList<VPContact>();
	        ArrayList<VPContact> contactList = dataAdapter.contactList;
	        int rowcount1 = contactList.size();
	        for(int i=0;i<rowcount1;i++)
	        {
	        	VPContact _contact = contactList.get(i);
	        	if(_contact.isSelect()){
	        		//listContactCheck.add(_contact);
	        		contactSend.add(_contact);
			          
			         }
	        }
	    	Serializable _listSend = (Serializable)contactSend;
	    	
	    	Intent intentFatherGroup = new Intent();
	    	intentFatherGroup.putExtra(CONTACT_LIST_CHECK, _listSend);
			setResult(2,intentFatherGroup);
		   	finish();
		   	
	      break;
	    case R.id.action_cancel:
	    	finish();
			    
	      	break;
	    default:
	    	break;
	    }

	    return true;
	  } 
	;

	
	//---------------------------------------------------------------------//
	 class MyCheckListAdapter extends ArrayAdapter<VPContact> {
		  
		  private ArrayList<VPContact> contactList;
		  int textViewResourceId;
		  public MyCheckListAdapter(Context context, int _textViewResourceId,
		    ArrayList<VPContact> contactList) {
		   super(context, _textViewResourceId, contactList);
		   this.contactList = new ArrayList<VPContact>();
		   this.contactList.addAll(contactList);
		   this.textViewResourceId = _textViewResourceId;
		  }
		  
		  private class ViewHolder {
		   TextView  id;
		   TextView name;
		   TextView email;
		   ImageView img;
		   CheckBox check;
		   
		  }
		  
		  @Override
		  public View getView(int position, View convertView, ViewGroup parent) {
		  
		   ViewHolder holder = null;
		   Log.v("ConvertView", String.valueOf(position));
		   byte [] imagebyte = null;
		   if (convertView == null) {
		   LayoutInflater vi = (LayoutInflater)getSystemService(
		     Context.LAYOUT_INFLATER_SERVICE);
		   convertView = vi.inflate(textViewResourceId, null);
		  
		   holder = new ViewHolder();
		   holder.id = (TextView) convertView.findViewById(R.id.textView_ID_chose);
		   holder.name = (TextView) convertView.findViewById(R.id.textView_HoTen_choose);
		   holder.email = (TextView) convertView.findViewById(R.id.textView_Email_choose);
		   holder.img = (ImageView) convertView.findViewById(R.id.image_Contact_choose);
		   holder.check = (CheckBox) convertView.findViewById(R.id.checkbox_Contact_choose);
		   convertView.setTag(holder);
		  
		    holder.check.setOnClickListener( new View.OnClickListener() {
		     public void onClick(View v) {
		      CheckBox cb = (CheckBox) v ;
		      VPContact _contact = (VPContact) cb.getTag();
		       _contact.setSelect(cb.isChecked());
		     }
		    });
		   }
		   else {
		    holder = (ViewHolder) convertView.getTag();
		   }
		  
		   VPContact _contact = contactList.get(position);
		   holder.id.setText(String.valueOf(_contact.getVpcID()));
		   holder.name.setText(_contact.getVpcLastName()+" "+_contact.getVpcFistName());
		   holder.email.setText(_contact.getVpcEmail());
		   
		   if (_contact.getVpcImage()!= null) {
           	imagebyte = _contact.getVpcImage();
           	holder.img.setImageBitmap(BitmapFactory.decodeByteArray(imagebyte, 0, imagebyte.length));
			} else {
	           
				holder.img.setImageResource(R.raw.user1);
			}
           
		   holder.check.setChecked(_contact.isSelect());
		   holder.check.setTag(_contact);
		  
		   return convertView;
		  
		  }
		  
		 }

}
